package com.ruoyi.iot.controller;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.iot.dto.Sensor;
import com.ruoyi.iot.service.SensorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/iot/sensor")
@DataSource(DataSourceType.MASTER)
@Api(tags = {"物联网数据服务接口"}, value = "物联网设备总表信息数据服务")
public class SensorController {

    @Autowired
    private SensorService sensorService;

    @PostMapping
    @ApiOperation(value = "上传传感器数据接口")
    public R<String> upload(@RequestBody @Validated(Sensor.Upload.class) Sensor sensor){

        sensorService.upload(sensor);
        return R.ok("传感器数据上传成功");
    }

    @DeleteMapping
    @ApiOperation(value = "删除传感器数据接口")
    public R<String> delete(@RequestParam("sensorId") String sensorId){

        sensorService.delete(sensorId);
        return R.ok("传感器数据删除成功");
    }

    @GetMapping
    @ApiOperation(value = "查询传感器数据接口")
    public R<List<Sensor>> get(@RequestParam(name = "mineId", required = false) String mineId,
                      @RequestParam(name = "sensorType", required = false) String sensorType,
                      @RequestParam(name = "runningState", required = false) Integer runningState){

        List<Sensor> sensorList = sensorService.search(mineId, sensorType, runningState);
        return R.ok(sensorList,"传感器数据查询成功");
    }

    @PutMapping
    @ApiOperation(value = "更新传感器数据接口")
    public R<String> update(@RequestBody @Validated(Sensor.Update.class) Sensor sensor){

        sensorService.update(sensor);
        return R.ok("传感器数据更新成功");
    }

    @GetMapping("/all")
    @ApiOperation(value = "查询所有传感器数据")
    public R<List<Sensor>> searchAll(){

        List<Sensor> sensorList = sensorService.searchAll();
        return R.ok(sensorList,"传感器数据查询成功");
    }
}
